package com.gxa.ssm251.proxy.cglib;


import com.gxa.ssm251.proxy.jdk.UserDao;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.Arrays;

@Component
public class UserDaoMethodInterceptor implements MethodInterceptor {

    @Resource
    UserDao userDao2=null;

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
   Object invoke=null;
    try {
        System.out.println("前置增强");   //openSession(),第一顺位执行
        //输出参数情况
        System.out.println("参数情况:"+ Arrays.toString(objects));
        invoke = method.invoke(userDao2, objects);
        System.out.println("后置返回增强"); //commit(), 第三顺位执行
         return  invoke;
    }catch (Exception e){
        System.out.println("后置抛出增强"); //rollback(), 第三顺位执行
    }finally {
        System.out.println("后置增强");  //session.close(),第二顺位执行
    }

        return invoke;
    }
}
